package com.yww.algorithm.sort;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author yww
 * @description 基数排序
 * @since 2024/2/25 11:25
 */
public class RadixSort {
    public static void sort(String[] arr, int len) {
        // 准备128个桶
        ArrayList<String>[] buckets = new ArrayList[128];
        for (int i = 0; i < buckets.length; i++) {
            buckets[i] = new ArrayList<>();
        }

        for (int i = len - 1; i >= 0; i--) {
            for (String s : arr) {
                buckets[s.charAt(i)].add(s);
            }
            int k = 0;
            for (ArrayList<String> bucket : buckets) {
                for (String s : bucket) {
                    arr[k++] = s;
                }
            }
        }
    }

    public static void main(String[] args) {
        String[] a = {"6", "5", "4", "3","2", "1"};
        System.out.println(Arrays.toString(a));
        sort(a,1);
        System.out.println(Arrays.toString(a));
    }
}
